package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.TemplateDto;
import io.geekidea.boot.shop.entity.Template;
import io.geekidea.boot.shop.mapper.TemplateMapper;
import io.geekidea.boot.shop.query.app.AppTemplateQuery;
import io.geekidea.boot.shop.query.web.TemplateQuery;
import io.geekidea.boot.shop.service.base.api.TemplateService;
import io.geekidea.boot.shop.vo.app.AppTemplateVo;
import io.geekidea.boot.shop.vo.web.TemplateVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网页模板表 服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {

    @Autowired
    private TemplateMapper templateMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addTemplate(TemplateDto dto) {
        Template template = new Template();
        BeanUtils.copyProperties(dto, template);
        return save(template);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTemplate(TemplateDto dto) {
        String id = dto.getId();
        Template template = getById(id);
        if (template == null) {
            throw new BusinessException("网页模板表不存在");
        }
        BeanUtils.copyProperties(dto, template);
        return updateById(template);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTemplate(String id) {
        return removeById(id);
    }

    @Override
    public TemplateVo getTemplateById(String id) {
        return templateMapper.getTemplateById(id);
    }

    @Override
    public Paging<TemplateVo> getTemplatePage(TemplateQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<TemplateVo> list = templateMapper.getTemplatePage(query);
        Paging<TemplateVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppTemplateVo getAppTemplateById(String id) {
        return templateMapper.getAppTemplateById(id);
    }

    @Override
    public Paging<AppTemplateVo> getAppTemplatePage(AppTemplateQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppTemplateVo> list = templateMapper.getAppTemplatePage(query);
        Paging<AppTemplateVo> paging = new Paging<>(list);
        return paging;
    }

}
